package com.code.soulspringboot.handler;

import com.code.soulspringboot.code.BizResponseCode;
import com.code.soulspringboot.exception.BaseException;
import com.code.soulspringboot.exception.PointException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 处理方式一：这里统一处理，controller需要调用此方法
 * 处理方式二：使用spring Boot提供的统一异常处理方式
 * 对异常统一处理；
 * @author lyf
 * @date 2022-11-21 23:02
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionAdvice {

    @ExceptionHandler(PointException.class)
    public BaseResponse<Void> pointExceptionHandler(PointException pointException) {
        log.error("pointException：", pointException);
        return new BaseResponse<>(pointException.getErrorCode().getCode(), pointException.getErrorMsg());
    }

    @ExceptionHandler(BaseException.class)
    public BaseResponse<Void> baseExceptionHandler(BaseException baseException) {
        log.error("baseException: ", baseException);
        return new BaseResponse<>(baseException.getErrorCode().getCode(), baseException.getErrorMsg());
    }

    @ExceptionHandler(Exception.class)
    public BaseResponse<Void> exceptionHandler(Exception exception) {
        log.error("exception: ", exception);
        return new BaseResponse<>(BizResponseCode.SYS_ERROR.getCode(), BizResponseCode.SYS_ERROR.getMsg());
    }
}
